/**
 * Created by MD-3 on 2017/6/9.
 */
function initmap() {
    var lng  = initmap_mock.centerLng;
    var lat  = initmap_mock.centerLat;
    var zoom = initmap_mock.centerZoom;

    <!-- id="container"固定，不能修改 -->
    var map = new AMap.Map('container', {
        resizeEnable: true,                                //是否监控地图容器尺寸变化，默认值为false
        zoom: zoom,                                           //地图显示的缩放级别，若center与zoom未赋值，地图初始化默认显示用户所在城市范围
        center: [lng, lat],                                   //地图中心点坐标值
    });
    return map;
}

function ContextMenu() {
    var me = this;
    this.mouseTool = new AMap.MouseTool(map); //地图中添加鼠标工具MouseTool插件，script需要添加plugin=AMap.MouseTool
    var content = [];
    content.push("<div>");
    content.push("    <ul class='context_menu'>");
    content.push("        <li onclick='menu.zoomMenu(0)'>缩小</li>");
    content.push("        <li onclick='menu.zoomMenu(1)'>放大</li>");
    content.push("        <li class='split_line' onclick='menu.rectZoomInMenu()'>区域放大</li>");
    content.push("        <li onclick='menu.distanceMeasureMenu()'>距离量测</li>");
    content.push("        <li class='split_line' onclick='menu.measureAreaMenu()'>面积量测</li>");
    content.push("        <li onclick='menu.exitMenu()'>退出</li>");
    content.push("    </ul>");
    content.push("</div>");
    this.contextMenu = new AMap.ContextMenu({isCustom: true, content: content.join('')});//通过content自定义右键菜单内容
    this.contextMenuPositon = null;
    //地图绑定鼠标右击事件——弹出右键菜单
    map.on('rightclick', function (e) {
        me.contextMenu.open(map, e.lnglat);
        me.contextMenuPositon = e.lnglat; //右键菜单位置
    });
}
ContextMenu.prototype.zoomMenu = function zoomMenu(tag) {//右键菜单缩放地图
    if (tag === 0) {
        map.zoomOut();
    }
    if (tag === 1) {
        map.zoomIn();
    }
    this.contextMenu.close();
}
ContextMenu.prototype.rectZoomInMenu = function () {
    this.mouseTool.rectZoomIn();
    map.setDefaultCursor("crosshair");                     //鼠标样式图标
    this.contextMenu.close();
}
ContextMenu.prototype.distanceMeasureMenu = function () {  //右键菜单距离量测
    this.mouseTool.rule();
    map.setDefaultCursor("crosshair");
    this.contextMenu.close();
}
ContextMenu.prototype.measureAreaMenu = function () {  //右键菜单面积量测
    this.mouseTool.close();
    this.mouseTool.measureArea();
    map.setDefaultCursor("crosshair");
    this.contextMenu.close();
}
ContextMenu.prototype.exitMenu = function () {
    this.mouseTool.close();
    map.setDefaultCursor("url(http://webapi.amap.com/theme/v1.3/openhand.cur),default");
    //map.setDefaultCursor("pointer");
    this.contextMenu.close();
}


//添加常用控件或服务
//address：市、区
function addMapControls() {
    /*  map.plugin(["AMap.ToolBar"],function(){           //地图操作工具条控件 - 样式1
     var tool = new AMap.ToolBar();
     map.addControl(tool);
     });*/
    AMapUI.loadUI(['control/BasicControl'], function (BasicControl) {        //地图缩放控件 - 样式2
        var zoomCtrl = new BasicControl.Zoom({
            position: {
                bottom: '80px',
                left: '10px'
            }
        });
        map.addControl(zoomCtrl);
    });

    map.plugin(["AMap.Scale"], function () {             //比例尺控件
        var scale = new AMap.Scale({position: "LB"});    //停靠位置，LT:左上角; RT:右上角; LB:左下角; RB:右下角; 默认位置：LB
        map.addControl(scale);
    });

    map.plugin(["AMap.OverView"], function () {         //地图鹰眼控件
        var view = new AMap.OverView();
        map.addControl(view);
    });

    mapLayerSwitcher();                                  //图层切换
}

//图层切换
function mapLayerSwitcher() {
    AMapUI.loadUI(['control/BasicControl'], function (BasicControl) {
        var layerCtrl = new BasicControl.LayerSwitcher({
            position: {
                bottom: '170px',
                left: '10px'
            },
            //theme: 'dark',
            //自定义基础图层
            baseLayers: [{                                //底图图层列表，每次只启用一个
                id: 'tile',                                 //图层id，需唯一
                enable: true,                               //是否启用
                name: '标准图',                              //显示名称，html
                layer: new AMap.TileLayer()                 //图层实例,切片图层类
            }, {
                id: 'satellite',
                name: '卫星图',
                layer: new AMap.TileLayer.Satellite()      //卫星图层类，继承自TileLayer
            }],
            //自定义覆盖图层
            overlayLayers: [{                          //覆盖物图层列表，可同时启用多个
                id: 'traffic',
                name: '交通图',
                layer: new AMap.TileLayer.Traffic()       //实时交通图层类，继承自TileLayer
            }, {
                id: 'roadNet',
                name: '路网图',
                layer: new AMap.TileLayer.RoadNet()       //路网图层类，继承自TileLayer
            }, {
                id: 'buildings',
                name: '3D楼块图',
                layer: new AMap.Buildings()
            }]
        });
        map.addControl(layerCtrl);
    });
}




//查询实时天气信息, 查询的城市到行政级别的城市，如朝阳区、杭州市
function weatherService() {
    //var address,weatherTipId,weatherLiveId,weatherForecastId;
    var weatherTitleId = "myweather-title";
    var address = initmap_mock.address;

    AMap.service('AMap.Weather', function () {
        var weather = new AMap.Weather();
        //查询实时天气信息, 查询的城市到行政级别的城市，如朝阳区、杭州市
        weather.getLive(address, function (err, data) {
            if (!err) {
                var str = [];
                str.push('[' + data.city + ']');
                str.push('天气：' + data.weather);
                str.push('，温度：' + data.temperature + '℃');
                document.getElementById(weatherTitleId).innerHTML = str.join('');
            }
        });
    });
}

//查询实时天气信息, 查询的城市到行政级别的城市，如朝阳区、杭州市
function weatherInfo() {
    var address = initmap_mock.address;
    var weatherLiveId = "myweather-live";
    var weatherForecastId = "myweather-forecast";

    if (document.getElementById(weatherLiveId).style.display == "block") {
        document.getElementById(weatherLiveId).style.display = "none";
        document.getElementById(weatherForecastId).style.display = "none";
        return;
    }
    AMap.service('AMap.Weather', function () {
        var weather = new AMap.Weather();
        //查询实时天气信息, 查询的城市到行政级别的城市，如朝阳区、杭州市
        weather.getLive(address, function (err, data) {
            if (!err) {
                var str = [];
                str.push('<div style="color: #3366FF;">实时天气' + '</div>');
                str.push('<div>城市/区：' + data.city + '</div>');
                str.push('<div>天气：' + data.weather + '</div>');
                str.push('<div>温度：' + data.temperature + '℃</div>');
                str.push('<div>风向：' + data.windDirection + '</div>');
                str.push('<div>风力：' + data.windPower + ' 级</div>');
                str.push('<div>空气湿度：' + data.humidity + '</div>');
                str.push('<div>发布时间：' + data.reportTime + '</div>');
                document.getElementById(weatherLiveId).innerHTML = str.join('');
                document.getElementById(weatherLiveId).style.display = "block";
            }
        });
        //未来4天天气预报
        weather.getForecast(address, function (err, data) {
            if (err) {
                return;
            }
            var str = [];
            str.push('<div style="color: #3366FF;">[' + address + ']未来4天天气' + '</div>')
            for (var i = 0, dayWeather; i < data.forecasts.length; i++) {
                dayWeather = data.forecasts[i];       //Forecast 对象:白天天气现象,夜间天气现象,白天温度,夜间温度,白天风向,夜间风向,白天风力,夜间风力
                str.push('<div>' + dayWeather.date + ' <div class="weather">' + dayWeather.dayWeather + '</div> ' + dayWeather.nightTemp + '~' + dayWeather.dayTemp + '℃' + '</div>');
            }
            document.getElementById(weatherForecastId).innerHTML = str.join('');
            document.getElementById(weatherForecastId).style.display = "block";
        });
    });
}